package com.cs.bd.ad.http.bean;

import android.content.Context;
import android.text.TextUtils;

import com.cs.bd.ad.bean.AdModuleInfoBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BaseOnlineModuleInfoBean {
    private static final String DEFAULT_VALUE_STRING = "";
    private static final int DEFAULT_VALUE_INT = 0;
    private int mAdPos;
    private List<BaseOnlineAdInfoBean> mOnlineAdInfoList;
    private long mSaveDataTime;
    private String mHasShowAdUrlList;

    public BaseOnlineModuleInfoBean() {
    }

    public int getAdPos() {
        return this.mAdPos;
    }

    public void setAdPos(int adPos) {
        this.mAdPos = adPos;
    }

    public long getSaveDataTime() {
        return this.mSaveDataTime;
    }

    public void setSaveDataTime(long saveDataTime) {
        this.mSaveDataTime = saveDataTime;
    }

    public void setHasShowAdUrlList(String hasShowAdUrlList) {
        this.mHasShowAdUrlList = hasShowAdUrlList;
    }

    public String getHasShowAdUrlList() {
        return this.mHasShowAdUrlList;
    }

    public List<BaseOnlineAdInfoBean> getOnlineAdInfoList() {
        return this.mOnlineAdInfoList;
    }

    public void setOnlineAdInfoList(List<BaseOnlineAdInfoBean> onlineAdInfoList) {
        this.mOnlineAdInfoList = onlineAdInfoList;
    }

    public static BaseOnlineModuleInfoBean parseJsonObject(Context context, int adPos, int advDataSource, int virtualModuleId, int moduleId, JSONObject datasJsonObj) {
        if (datasJsonObj != null && datasJsonObj.length() >= 1 && datasJsonObj.has(String.valueOf(adPos))) {
            BaseOnlineModuleInfoBean onlineModuleInfoBean = new BaseOnlineModuleInfoBean();
            onlineModuleInfoBean.mAdPos = adPos;

            try {
                onlineModuleInfoBean.mOnlineAdInfoList = BaseOnlineAdInfoBean.parseJsonArray(context, datasJsonObj.getJSONArray(String.valueOf(adPos)), virtualModuleId, moduleId, adPos, advDataSource);
            } catch (Exception var8) {
                var8.printStackTrace();
            }

            if (datasJsonObj.has("saveDataTime")) {
                onlineModuleInfoBean.mSaveDataTime = datasJsonObj.optLong("saveDataTime", 0L);
            }

            if (datasJsonObj.has("hasShowAdUrlList")) {
                onlineModuleInfoBean.mHasShowAdUrlList = datasJsonObj.optString("hasShowAdUrlList", "");
            }

            return onlineModuleInfoBean;
        } else {
            return null;
        }
    }

    public static boolean saveAdDataToSdcard(int adPos, JSONObject cacheDataJsonObject) {
        if (cacheDataJsonObject != null && cacheDataJsonObject.length() >= 1) {
            if (!cacheDataJsonObject.has("saveDataTime")) {
                try {
                    cacheDataJsonObject.put("saveDataTime", System.currentTimeMillis());
                } catch (Exception var4) {
                    var4.printStackTrace();
                }
            }

//            try {
//                return FileCacheUtils.saveCacheDataToSdcard(getCacheFileName(adPos), StringUtils.toString(cacheDataJsonObject), true);
//            } catch (Exception var3) {
//                var3.printStackTrace();
//                return false;
//            }
        } else {
        }
        return false;
    }

    public static AdModuleInfoBean getOnlineAdInfoList(Context context, BaseModuleDataItemBean moduleDataItemBean, int adPos, int adCount, boolean needShownFilter, List<String> installFilterException, JSONObject onlineAdJsonObj) {
        int virtualModuleId = -1;
        int moduleId = -1;
        if (moduleDataItemBean != null) {
            virtualModuleId = moduleDataItemBean.getVirtualModuleId();
            moduleId = moduleDataItemBean.getModuleId();
        }

        BaseOnlineModuleInfoBean onlineModuleInfoBean = parseJsonObject(context, adPos, moduleDataItemBean != null ? moduleDataItemBean.getAdvDataSource() : 0, virtualModuleId, moduleId, onlineAdJsonObj);
        List<BaseOnlineAdInfoBean> onlineAdInfoList = onlineModuleInfoBean != null ? onlineModuleInfoBean.getOnlineAdInfoList() : null;
        List<BaseOnlineAdInfoBean> adAppInfoList = null;
        Iterator var13;
        BaseOnlineAdInfoBean onlineAdInfoBean;
        if (onlineAdInfoList != null && !onlineAdInfoList.isEmpty()) {
            String hasShowAdUrls = onlineModuleInfoBean.getHasShowAdUrlList();
//            if (LogUtils.isShowLog()) {
//                LogUtils.d("Ad_SDK", "[vmId:" + virtualModuleId + "]getOnlineAdInfoList(adPos::->" + adPos + ", hasShowAdUrls::->" + hasShowAdUrls + ")");
//            }

            var13 = onlineAdInfoList.iterator();

            label86:
            do {
//                do {
//                    do {
//                        do {
//                            if (!var13.hasNext()) {
//                                break label86;
//                            }
//
//                            onlineAdInfoBean = (BaseOnlineAdInfoBean)var13.next();
//                        } while(onlineAdInfoBean == null);
//                    } while(needShownFilter && !TextUtils.isEmpty(hasShowAdUrls) && hasShowAdUrls.indexOf("||" + onlineAdInfoBean.getTargetUrl() + "||") >= 0);
//                } while((installFilterException == null || !installFilterException.contains(onlineAdInfoBean.getPackageName())) && AppUtils.isAppExist(context, onlineAdInfoBean.getPackageName()));

                if (adAppInfoList == null) {
                    adAppInfoList = new ArrayList();
                }

//                adAppInfoList.add(onlineAdInfoBean);
            } while(adCount <= 0 || adAppInfoList.size() < adCount);
        }

        if (adAppInfoList != null && !adAppInfoList.isEmpty()) {
            AdModuleInfoBean adInfoBean = new AdModuleInfoBean();
            adInfoBean.setOnlineAdInfoList(context, moduleDataItemBean, onlineModuleInfoBean, adAppInfoList, installFilterException);
//            if (LogUtils.isShowLog()) {
//                var13 = adAppInfoList.iterator();
//
//                while(var13.hasNext()) {
//                    onlineAdInfoBean = (BaseOnlineAdInfoBean)var13.next();
//                    if (onlineAdInfoBean != null) {
//                        LogUtils.d("Ad_SDK", "return online ad info::>(count:" + adAppInfoList.size() + "--" + adCount + ", VirtualModuleId:" + onlineAdInfoBean.getVirtualModuleId() + ", ModuleId:" + onlineAdInfoBean.getModuleId() + ", MapId:" + onlineAdInfoBean.getMapId() + ", packageName:" + onlineAdInfoBean.getPackageName() + ", Name:" + onlineAdInfoBean.getAppName() + ", AdPos:" + onlineAdInfoBean.getAdPos() + ")");
//                    }
//                }
//            }

            return adInfoBean;
        } else {
            return null;
        }
    }

    public static boolean checkOnlineAdInfoValid(long loadDataTime) {
        return loadDataTime <= 0L || loadDataTime > System.currentTimeMillis() - 3600000L;
    }

    public static String getCacheFileName(int adPos) {
        return "online_ad_" + adPos;
    }
}
